package com.sjs.boot.dynamic.facade.config;

import com.sjs.boot.dynamic.core.config.method.MethodConfig;
import com.sjs.boot.dynamic.facade.run.AbstractHandler;
import com.sjs.boot.dynamic.facade.run.AbstractProcessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * @author uto_dev_008
 * @desc
 * @since 2021/11/9 19:34
 * Copyright (c) 2021 南京友拓信息技术有限公司 All rights reserved
 */
public abstract class AbstractProcessConfig {
    private static final Logger LOG = LoggerFactory.getLogger(AbstractProcessConfig.class);


    /**
     * Override it to parse more information if needed
     * @param ver
     * @param json
     * @return
     */
    abstract protected boolean parseExt(String ver, Map<String, Object> json, MethodConfig mc);

    public boolean parseConfig(String version, Map<String, Object> json, MethodConfig mc) {
        if(json == null) {
            if(isCanBeNull()) {
                return true; //使用默认的配置
            }
            LOG.error("process config can't be null in {}", mc.getName());
            return false;
        }
        AbstractProcessor processor = (AbstractProcessor) AbstractHandler.getHandler(json, mc.getProcessor());

        if(processor == null) {
            LOG.error("Fail to parse processor-config of {}", mc.getName());
            return false;
        }

        mc.setProcessor(processor);
        if(!parseExt(version, json, mc)) {
            LOG.error("Fail to parse process config");
            return false;
        }
        return true;
    }

    //override it when need it
    public void destroy() {
    }

    /**
     * process配置是否可以使用默认值
     * @return
     */
    protected boolean isCanBeNull() {
        return true;
    }

}
